package com.example.springboot.config.interceptors;

import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * MDC中保存用户，然后在logback-spring.xml中取出来，通过%X{userId}的占位符
 *
 * @Author:ChenZhangKun
 * @Date: 2021/9/22 10:30
 */
public class LogMdcInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 这里直接记录用户为czk
        MDC.put("userId", "czk");
        return true;
    }
}
